import { _decorator, CircleCollider2D, Collider2D, Component, Contact2DType,debug,Director,director,instantiate,Node, ParticleSystem2D, Prefab, resources, Sprite, SpriteFrame, v3, Vec2, Vec3} from 'cc';
import { StaticResources } from './utils/StaticResources';
const { ccclass, property } = _decorator;

@ccclass('emojiScript')
export class emojiScript extends Component {
    @property(Prefab) emoji:Prefab
    @property(Prefab) emojiParticle:Prefab
    emojis:SpriteFrame[]
    async start() {
        //加载所有的emoji图片
        this.emojis = await StaticResources.getAllEmoji()
        this.initEvent()
    }
    //初始化碰撞事件
    initEvent()
    {
        this.node.getComponent(CircleCollider2D).on(Contact2DType.BEGIN_CONTACT,this.contactStart,this)
    }
    //监听碰撞事件
    contactStart(selfCollider: Collider2D, otherCollider: Collider2D)
    {
        //获取碰撞节点
        const selfNode = selfCollider.node;
        const otherNode = otherCollider.node;
        //同tag值节点碰撞检测
        if(selfCollider.tag === otherCollider.tag && selfCollider.tag < 10 && selfNode.uuid < otherNode.uuid)
        {
            const tag = selfCollider.tag
            //计算emoji连线中心点
            const dx = (selfNode.position.x - otherNode.position.x) / 2
            const x = otherNode.position.x + dx
            const dy = (selfNode.position.y - otherNode.position.y) / 2
            const y = otherNode.position.y + dy
            //在两个emoji中心连线的中电生成新的emoji
            this.createEmoji(this.emojis[tag],v3(x,y,0),v3(tag+2,tag+2,tag+2),tag+1)
            //创建粒子
            this.createParticle(v3(x,y,0),tag*100,tag*20,tag*40)
            //销毁两个同tag值的emoji
            this.destroyNode([selfNode,otherNode])
        }
        //将顶部emoji碰撞组转换成容器内emoji碰撞组
        if(otherCollider.tag >= -1 && selfCollider.group === 8)
        {
            selfCollider.group = 16
        }
    }
    //用于销毁节点
    destroyNode(node:Node[])
    {
        director.once(Director.EVENT_AFTER_PHYSICS,()=>{
            for(let i = 0;i<node.length;i++)
            {
                node[i].destroy()
            }
        },this)
    }
    //根据tag值创建对应emoji
    createEmoji(img:SpriteFrame,position:Vec3,scale:Vec3,tag:number)
    {
        director.once(Director.EVENT_AFTER_UPDATE,()=>{
            const emoji = instantiate(this.emoji)
            emoji.getComponent(Sprite).spriteFrame = img
            emoji.setScale(scale)
            emoji.getComponent(CircleCollider2D).tag = tag
            emoji.setPosition(position)
            this.node.parent.addChild(emoji)
        })
    }
    //创建粒子
    createParticle(position:Vec3,speed?:number,startSize?:number,endSize?:number)
    {
        const particle = instantiate(this.emojiParticle)
        this.node.parent.parent.addChild(particle)
        particle.setPosition(position)
        const particleConfig = particle.getComponent(ParticleSystem2D)
        particleConfig.speed = speed
        particleConfig.startSize = startSize
        particleConfig.endSize = endSize
    }

    //超出屏幕外销毁自己
    destroyThis()
    {
        if(this.node.worldPosition.y<0)
        {
            this.destroyNode([this.node])
        }
    }
    update(deltaTime: number) {
        this.destroyThis()
    }
}


